#encoding:utf8
"""
Argument Dispatcher
"""

import argparse

##########################################################################

class ArgumentDispatcher(object):
    '''
    ArgumentDispatcher
    '''
    def __init__(self, default_processor= None):
        self._parser            = argparse.ArgumentParser()
        self._default_processor = default_processor

    def add(self, prefix, processor):
        '''
        add_argument
        '''
        assert isinstance(prefix, str), 'prefix should be of type str'
        self._parser.add_argument(prefix, dest= 'execute_command', action= 'store_const', const= processor, help= processor.__doc__)

    def run(self):
        '''
        parse args and execute the specific command
        '''
        args    = self._parser.parse_args()
        if hasattr(args, 'execute_command') and not args.execute_command is None:
            # 打印处理函数的帮助信息
            helpText    = args.execute_command.__doc__
            print(helpText)

            # 执行处理函数
            args.execute_command()

        elif self._default_processor:
            self._default_processor()
        else:
            self._parser.print_help()

##########################################################################

if __name__=='__main__':
    pass
